<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>显示结果过滤器</title>
    <script src="./lib/vue-2.6.12.js"></script>
    <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
</head>

<body>
    <div id="app">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label>Id:
                    <input type="text" class="form-control" v-model="id">
                </label>
                <label>Name:
                    <!-- 按键修饰符，响应指定按键事件。系统只定义了enter，delete等很少几个 -->
                    <input type="text" class="form-control" v-model="name" @keyup.F2="add">
                </label>
                <button type="button" class="btn btn-primary" @click="add">添加</button>
            </div>
        </div>

        <table class="table table-hover table-bordered table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>名称</th>
                    <th>时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td>{{ item.id}}</td>
                    <td v-text="item.name"></td>
                    <td>{{item.ctime | dateFormat(false)}}</td>
                    <td> <a href="" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>
    </div>

    <!-- 私有过滤器 -->
    <div id="app2">
        <h3>{{ date | dateFormat }}</h3>
    </div>

    <script>
        // 全局过滤器，修改数据显示格式、甚至显示内容
        Vue.filter('dateFormat', function (dataStr, isShort) {
            let dt = new Date(dataStr);
            let y = dt.getFullYear();
            let m = dt.getMonth();
            let d = dt.getDate();

            if (isShort) {
                return `${y}-${m}-${d}`;
            } else {
                let hh = dt.getHours();
                let mm = dt.getMinutes();
                let ss = dt.getSeconds();
                return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
            }
        })

        // 自定义全局键盘修饰符
        Vue.config.keyCodes.F2 = 113


        let vm = new Vue({
            el: '#app',
            data: {
                id: '',
                name: '',
                keyword: '',
                list: [
                    { id: 1, name: '奔驰', ctime: new Date() },
                    { id: 2, name: '宝马', ctime: new Date() },
                    { id: 3, name: '奔奔', ctime: new Date() }
                ]
            },
            methods: {
                add() {
                    let car = { id: this.id, name: this.name, ctime: new Date() };
                    this.list.push(car);
                    this.id = this.name = '';
                },
                del(id) {
                    let idx = this.list.findIndex(item => {
                        if (item.id == id) {
                            return true;
                        }
                    })
                    this.list.splice(idx, 1);
                }
            }
        })

        let vm2 = new Vue({
            el: '#app2',
            data: {
                date: new Date()
            },
            filters: {  //私有的过滤器
                dateFormat(dataStr, isShort) {
                    let dt = new Date(dataStr);
                    let y = dt.getFullYear();
                    let m = dt.getMonth().toString().padStart(2, '0');
                    let d = dt.getDate().toString().padStart(2, '0');;

                    if (isShort) {
                        return `${y}年${m}月${d}日`;
                    } else {
                        let hh = dt.getHours().toString().padStart(2, '0');;
                        let mm = dt.getMinutes().toString().padStart(2, '0');;
                        let ss = dt.getSeconds().toString().padStart(2, '0');;
                        return `${y}年${m}月${d}日 ${hh}时${mm}分${ss}秒`;
                    }
                }
            }
        })     
    </script>
</body>

</html>